package com.beichuan_travel.utils;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Component
public class ImgUtil {

    public static final String HOME = System.getProperty("user.dir");

    private String rename(String originName) {
        String uuid = UUID.randomUUID().toString();
        uuid = uuid.replace("-", "");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy MM dd HH mm ss");
        String time = simpleDateFormat.format(new Date());
        time = time.replace("-", "");
        time.replace(" ","");
        return uuid + time + originName;
    }

    public void transfor(MultipartFile multipartFile, String url) {
        File file;
        try {
            file = new File(url);
            file.mkdirs();
            multipartFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String renameUrl(String dirName, String originName) {
        String name = rename(originName);
        return HOME + "/img/" + dirName + "/" + name;
    }
}
